// pages/lease/lease.js
const http = require("../../api/goods")
const {
  baseURL
} = require("../../utils/request")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    baseURL,
    userId: wx.getStorageSync('userInfo').userId,
    orderList: [],
  },

  // 弹窗的展示与隐藏
  onClose() {
    this.setData({
      show: false
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

  getOrderList() {
    http.getOrderList({
      userId: this.data.userId
    }).then(res => {
      console.log(res.data, "订单信息")
      if (res.code == 200) {
        var _orderList = res.data;
        for (var index in _orderList) {
          var orderDetailList = _orderList[index].orderDetailList;
          for (var i in orderDetailList) {
            var goodsImgArray = orderDetailList[i].goodsImg.split(',');
            if (goodsImgArray.length > 0) {
              orderDetailList[i].goodsImg = goodsImgArray[0];
            }
          }
        }
        this.setData({
          orderList: _orderList
        });
        console.log("orderList:", this.data.orderList);
      }
    });
  },

  //打开弹窗
  openView(e){
    let {
      orderId
    } = e.currentTarget.dataset;
    this.getOrder({orderId})
    this.setData({
      show: true
    })
  },

  WriteOff(event) {
    let {
      orderId
    } = event.currentTarget.dataset;
    this.qrCode(orderId);
    
  },

  getOrder(query){
    http.getOrderList(query).then(res=>{
      if (res.code == 200) {
        console.log(res.data,"查询到的订单数据");
        this.setData({
          qrCode:res.data[0].qrCode
        })
      }
    })
  },

  qrCode(orderId) {
    http.qrCode(orderId).then(res => {
      if (res.code == 200)
        console.log('二维码生成成功');
        this.getOrderList();
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getOrderList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})